package 
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Shape;
	
	/**
	 * ...
	 * @author Lokiraze
	 */
	public class Cell extends MovieClip 
	{
		public var highlight:Shape = new Shape();
		
		public var state:String = "";
		public var answer:int;
		public var playerAnswer:int;
		
		public var region:int;
		public var row:int;
		public var column:int;
		public var xPos:int;
		public var yPos:int;
		
		public var inFocus:Boolean;
		public var revealed:Boolean;
		public var isRight:Boolean;
		 
		public function Cell(x:int, y:int)
		{
			this.xPos = x;
			this.yPos = y;
			this.answer = 0;
			txtCell.selectable = false;
			GetGridInfo();
			this.Draw();
			this.DrawHighlight();
			addChild(highlight);
		}
		
		public function Draw()
		{		
			switch (state)
			{
				case "Initial":
					txtCell.textColor = 0x000000;
					txtCell.text = String(answer);
					txtCell.selectable = false;
					break;
				case "Hint":
					txtCell.textColor = 0x0000FF;
					txtCell.text = String(answer);
					txtCell.selectable = false;
					break;
				case "Answer":
					txtCell.textColor = 0x00FF00;
					txtCell.text = String(playerAnswer);
					txtCell.selectable = false;
					break;
				case "Wrong":
					txtCell.textColor = 0xFF0000;
					txtCell.text = String(playerAnswer);
					txtCell.selectable = false;
					break;
				default:
					txtCell.text = "";
					break;
			}
		}
		
		public function DrawHighlight():void
		{
			var g:Graphics = this.highlight.graphics;
			if (!inFocus)
			{
				g.lineStyle(1, 0x000000);
				g.lineTo(0, 0);
				g.lineTo(49, 0);
				g.lineTo(49, 49);
				g.lineTo(0, 49);
				g.lineTo(0, 0);
				g.endFill();
			}
			else
			{
				g.lineStyle(1, 0xffff00);
				g.lineTo(0, 0);
				g.lineTo(47, 0);
				g.lineTo(47, 47);
				g.lineTo(0, 47);
				g.lineTo(0, 0);
				g.endFill();
			}
		}
		
		public function GetGridInfo():void
		{
			 if(yPos < 3) // region 1, 2 or 3
			{
				if(xPos < 3)
				{
						this.region = 1;
						this.row = 0;
						this.column = 0;
				}
				else if(xPos >= 3 && xPos < 6)
				{
						this.region = 2;
						this.row = 3;
						this.column = 0;
				}
				else if(xPos >= 6)
				{
						this.region = 3;
						this.row = 6;
						this.column = 0;
				}
			}
			else if(yPos >= 3 && yPos < 6) // region 4, 5 or 6
			{
				if(xPos < 3)
				{
						this.region = 4;
						this.row = 0;
						this.column = 3;
				}
				else if(xPos >= 3 && xPos < 6)
				{
						this.region = 5;
						this.row = 3;
						this.column = 3;
				}
				else if(xPos >= 6)
				{
						this.region = 6;
						this.row = 6;
						this.column = 3;
				}
			}
			else if(yPos >= 6) // region 7, 8 or 9
			{
				if(xPos < 3)
				{
						this.region = 7;
						this.row = 0;
						this.column = 6;
				}
				else if(xPos >= 3 && xPos < 6)
				{
						this.region = 8;
						this.row = 3;
						this.column = 6;
				}
				else if(xPos >= 6)
				{
						this.region = 9;
						this.row = 6;
						this.column = 6;
				}
			}
		}
		
		public function setPlayerInput(s:int)
		{
			playerAnswer = s;
			state = "Answer";
			this.Draw();
		}
		
		public function Correction()
		{
			if (revealed)
			{
				isRight = true;
				return;
			}
			
			if (playerAnswer == answer)
			{
				isRight = true;
			}
			else
			{
				state = "Wrong"
				isRight = false;
				this.Draw();
			}
		}
		
		public function Right():Boolean
		{
			return isRight;
		}
		
		public function isFocused():Boolean
		{
			return inFocus;
		}
		
		public function isRevealed():Boolean
		{
			return revealed;
		}
		
		public function Highlight()
		{
			inFocus = true;
			this.Draw();
		}
		
		public function Unhighlight()
		{
			inFocus = false;
			this.Draw();
		}
		
		public function ShowAnswer(s:String)
		{
			revealed = true;
			state = s;
			this.Draw();
		}
		
		public function ResetCell()
		{
			revealed = false;
			state = "";
			this.Draw()
		}
		
		public function Debug()
		{
			trace(xPos + " , " + yPos + " , " + region +" , " + row + " , " + column + " , " + answer);
		}
	}
	
}